package org.jeecg.modules.demo.extend.extend.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.SelectTreeModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.extend.entity.BdMarbasclass;
import org.jeecg.modules.demo.extend.mapper.BdMarbasclassMapper;
import org.jeecg.modules.demo.extend.service.IBdMarbasclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhongll1
 * @ClassName BsMarbasclassController
 * @Description 物料分类扩展接口
 * @date 2024/3/8 14:52
 * @Version 1.0
 */
@Api(tags="物料基本分类")
@RestController
@RequestMapping("/extend/bdMarbasclass")
@Slf4j
public class MarbasclassExtendController extends JeecgController<BdMarbasclass, IBdMarbasclassService> {

    @Autowired
    private IBdMarbasclassService bdMarbasclassService;

    @Autowired
    private BdMarbasclassMapper baseMapper;

    /**根节点父ID的值*/
    public static final String ROOT_PID_VALUE = "0";

    /**
     * 查询所有的物料分类
     * @return
     */
    //@AutoLog(value = "物料基本分类-查询所有的物料分类")
    @ApiOperation(value="物料基本分类-查询所有的物料分类", notes="物料基本分类-查询所有的物料分类")
    @GetMapping(value = "/queryAllList")
    public Result<List<SelectTreeModel>> queryPageList() {

        Result<List<SelectTreeModel>> result = new Result<>();
        try {
            List<SelectTreeModel> ls =new ArrayList<>();
            //查出所有的一级节点
            ls = baseMapper.queryListByPid(ROOT_PID_VALUE,null);
            //遍历一级节点的所有子节点，添加进去
            ls.forEach(parent->{
                List<SelectTreeModel> selectTreeModels = queryListByPid(parent.getKey());
                parent.setChildren(selectTreeModels);
                loadAllChildren(selectTreeModels);
            });
            result.setResult(ls);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 递归加载子节点
     * @param ls
     */
    private void loadAllChildren(List<SelectTreeModel> ls) {
        for (SelectTreeModel tsm : ls) {
            List<SelectTreeModel> temp = bdMarbasclassService.queryListByPid(tsm.getKey());
            if (temp != null && temp.size() > 0) {
                tsm.setChildren(temp);
                loadAllChildren(temp);
            }
        }
    }

    /**
     * 根据父ID查询树节点信息
     * @param pid
     * @return
     */
    public List<SelectTreeModel> queryListByPid(String pid) {

        List<SelectTreeModel> models = baseMapper.queryListByPid(pid, null);
        if (CollectionUtil.isNotEmpty(models)){
            return models;
        }
        return models;
    }

}
